package cn.practice.weibopractice.controller;

import cn.practice.weibopractice.common.response.JsonResult;
import cn.practice.weibopractice.common.response.StatusCode;
import cn.practice.weibopractice.pojo.dto.WeiboDTO;
import cn.practice.weibopractice.pojo.entity.Weibo;
import cn.practice.weibopractice.pojo.mapper.WeiboMapper;
import cn.practice.weibopractice.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@Api(tags = "微博模块")
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    // 自动装配
    @Autowired
    private WeiboMapper weiboMapper;


    // 发布微博
    @PostMapping("insert")
    @ApiOperation("发布微博")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session) {
        log.debug("WeiboDto" + weiboDTO);

        //
        UserVO userVO =(UserVO) session.getAttribute("user");
//        if(userVO == null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }

        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO, weibo);
        weibo.setUserId(userVO.getId());
        weibo.setCreated(new Date());

        if(weiboMapper.insertWeibo(weibo) > 0){
            return JsonResult.success();
        }

        return JsonResult.fail();
    }

    // 获取微博列表
    @GetMapping("selectIndex")
    @ApiOperation("获取微博列表")
    public JsonResult selectIndex() {
        return JsonResult.success(weiboMapper.selectIndex());
    }

    // 微博详情
    @GetMapping("selectById")
    @ApiOperation("通过id微博详情")
    public JsonResult selectById(@RequestParam("id") long id,@RequestParam("username") String username) {
       log.debug("id" + id);

        return JsonResult.success(weiboMapper.selectById(id));
    }
}
